home *** CD-ROM | disk | FTP | other *** search
/ Night Owl 6 / Night Owl's Shareware - PDSI-006 - Night Owl Corp (1990).iso / 039a / dflat8.zip / HELP.C < prev    next >
Text File  |  1991-09-30  |  4KB  |  194 lines

  1. /* ------------ help.c ----------- */
  2.  
  3. #include <stdio.h>
  4. #include <string.h>
  5. #include <stdlib.h>
  6. #include "dflat.h"
  7.  
  8. int HelpBoxProc(WINDOW wnd, MESSAGE msg, PARAM p1, PARAM p2)
  9. {
  10.     switch (msg)    {
  11.         case CREATE_WINDOW:
  12.             SendMessage(wnd, CAPTURE_MOUSE, 0, 0);
  13.             SendMessage(wnd, CAPTURE_KEYBOARD, 0, 0);
  14.             break;
  15.         case LEFT_BUTTON:
  16.             if (WindowSizing || WindowMoving)
  17.                 return FALSE;
  18.             if (HitControlBox(wnd, p1-GetLeft(wnd), p2-GetTop(wnd)))    {
  19.                 PostMessage(wnd, KEYBOARD, ' ', ALTKEY);
  20.                 return TRUE;
  21.             }
  22.             break;
  23.         case KEYBOARD:
  24.             switch ((int) p1)    {
  25.                 case ALT_F4:
  26.                 case ESC:
  27.                     PostMessage(wnd, CLOSE_WINDOW, 0, 0);
  28.                     break;
  29.                 case ' ':
  30.                     if (p2 & ALTKEY)
  31.                         BuildSystemMenu(wnd);
  32.                     break;
  33.                 default:
  34.                     break;
  35.             }
  36.             break;
  37.         case CLOSE_WINDOW:
  38.             SendMessage(wnd, RELEASE_MOUSE, 0, 0);
  39.             SendMessage(wnd, RELEASE_KEYBOARD, 0, 0);
  40.             break;
  41.         default:
  42.             break;
  43.     }
  44.     return BaseWndProc(HELPBOX, wnd, msg, p1, p2);
  45. }
  46.  
  47. #define MAXHEIGHT 15
  48.  
  49. struct helps {
  50.     char *hname;
  51.     long hptr;
  52.     int hwidth;
  53.     int hheight;
  54.     struct helps *NextHelp;
  55. };
  56.  
  57. static struct helps *FirstHelp = NULL;
  58. static struct helps *LastHelp = NULL;
  59. static struct helps *ThisHelp = NULL;
  60.  
  61. static FILE *helpfp;
  62. static char hline [80];
  63. static char helpname[65];
  64.  
  65. /* ----------- load the help text file ------------ */
  66. void LoadHelpFile(char *expath)
  67. {
  68.     char *cp;
  69.     int first;
  70.  
  71.     UnLoadHelpFile();
  72.     strcpy(helpname, expath);
  73.     cp = strrchr(helpname, '\\');
  74.     if (cp == NULL)
  75.         cp = helpname;
  76.     strcpy(cp, "\\" DFLAT_APPLICATION ".HLP");
  77.     if ((helpfp = fopen(helpname, "rt")) == NULL)
  78.         return;
  79.     *hline = '\0';
  80.     while (*hline != '<')    {
  81.         if ((fgets(hline, 80, helpfp)) == NULL)    {
  82.             fclose(helpfp);
  83.                return;
  84.         }
  85.     }
  86.     while (*hline == '<')   {
  87.         if (strncmp(hline, "<end>", 5) == 0)
  88.             break;
  89.  
  90.         if ((ThisHelp = malloc(sizeof(struct helps))) == NULL)
  91.             break;
  92.  
  93.         if (FirstHelp == NULL)
  94.             FirstHelp = ThisHelp;
  95.  
  96.         if ((cp = strchr(hline, '>')) == NULL)
  97.             continue;
  98.         *cp = '\0';
  99.  
  100.         if ((ThisHelp->hname=malloc(strlen(hline+1)+1))==NULL)
  101.             break;
  102.  
  103.         /* ------- build the help linked list entry --- */
  104.         strcpy(ThisHelp->hname, hline+1);
  105.         ThisHelp->hptr = ftell(helpfp);
  106.         ThisHelp->hwidth = 0;
  107.         ThisHelp->hheight = 0;
  108.         ThisHelp->NextHelp = NULL;
  109.  
  110.         /* ------ append entry to the linked list ------ */
  111.         if (LastHelp != NULL)
  112.             LastHelp->NextHelp = ThisHelp;
  113.         LastHelp = ThisHelp;
  114.  
  115.         /* -------- move to the next <> token ------ */
  116.         if (fgets(hline, 80, helpfp) == NULL)
  117.             strcpy(hline, "<end>");
  118.         first = TRUE;
  119.         while (*hline != '<')    {
  120.             if (first)    {
  121.                 ThisHelp->hwidth = strlen(hline)+6;
  122.                 first = FALSE;
  123.             }
  124.             else 
  125.                 ThisHelp->hwidth =
  126.                     max(ThisHelp->hwidth, strlen(hline));
  127.             if (ThisHelp->hheight < MAXHEIGHT)
  128.                 ThisHelp->hheight++;
  129.             if (fgets(hline, 80, helpfp) == NULL)
  130.                 strcpy(hline, "<end>");
  131.         }
  132.     }
  133.     fclose(helpfp);
  134. }
  135.  
  136. /* ------ free the memory used by the help file table ------ */
  137. void UnLoadHelpFile(void)
  138. {
  139.     while (FirstHelp != NULL)    {
  140.         ThisHelp = FirstHelp;
  141.         free(ThisHelp->hname);
  142.         FirstHelp = ThisHelp->NextHelp;
  143.         free(ThisHelp);
  144.     }
  145.     ThisHelp = LastHelp = NULL;
  146. }
  147.  
  148. /* ------------ display help text ----------- */
  149. void DisplayHelp(char *Help)
  150. {
  151.     WINDOW wnd;
  152.  
  153.     if ((helpfp = fopen(helpname, "rt")) == NULL)
  154.        return;
  155.  
  156.     ThisHelp = FirstHelp;
  157.  
  158.     while (ThisHelp != NULL)    {
  159.         if (stricmp(Help, ThisHelp->hname) == 0)
  160.             break;
  161.         ThisHelp = ThisHelp->NextHelp;
  162.     }
  163.     if (ThisHelp != NULL)    {
  164.           fseek(helpfp, ThisHelp->hptr, 0);
  165.            fgets(hline, 80, helpfp);
  166.         hline[strlen(hline)-1] = '\0';
  167.         /* ----- create the help window ------ */
  168.         wnd = CreateWindow(HELPBOX,
  169.                             hline,
  170.                             -1, -1,
  171.                             ThisHelp->hheight+2,
  172.                             ThisHelp->hwidth+2,
  173.                             NULL,
  174.                             NULL,
  175.                             NULL,
  176.                             0);
  177.         if (wnd != NULLWND)    {
  178.             /* ----- read the help text ------- */
  179.             hline[strlen(hline)-1] = '\0';
  180.             while (TRUE)    {
  181.                 if (fgets(hline, 80, helpfp) == NULL)
  182.                     break;
  183.                 hline[strlen(hline)-1] = '\0';
  184.                 if (*hline == '<')
  185.                     break;
  186.                 /* --- add help text to the help window --- */
  187.                 SendMessage(wnd, ADDTEXT, LPARAM(hline), 0);
  188.             }
  189.             SendMessage(wnd, SETFOCUS, TRUE, 0);
  190.         }
  191.     }
  192.     fclose(helpfp);
  193. }
  194.